/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wxk.Bonus.Analyst;

import java.util.Hashtable;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.effect.Lighting;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import wxk.Bonus.Constant.GlobalConfig;
import wxk.Bonus.bean.ChinaDoubleBallBean;
import wxk.Bonus.utils.BonusLogger;

/**
 * Created on : Feb 2, 2015 6:08:08 PM
 * @author William Wang
 */

public class UnionLottoBI {
  private double initX;
    private double initY;
 Hashtable <String,Integer>summary =new Hashtable<String,Integer>();
 
 BonusLogger log = BonusLogger.getInstance();
 GlobalConfig globalconifg = GlobalConfig.getInstance();
     private TableView table = new TableView();
    private final ObservableList<Summary> data =
        FXCollections.observableArrayList();   
    
public UnionLottoBI()
{
}
public void summary()
{
 if(globalconifg.filterData==null){log.log("filterdata is null");return;   }
 summary.clear();
 System.out.println("cdbb:globalconifg.filterData.getSource():"+globalconifg.filterData.getSource().size());
 for(ChinaDoubleBallBean cdbb:globalconifg.filterData.getSource())
 {
     if(cdbb.isIsnext())
     {
         addNum("01", Integer.parseInt(cdbb.getRed1()));
         addNum("02", Integer.parseInt(cdbb.getRed2()));
         addNum("03", Integer.parseInt(cdbb.getRed3()));
         addNum("04", Integer.parseInt(cdbb.getRed4()));
         addNum("05", Integer.parseInt(cdbb.getRed5()));
         addNum("06", Integer.parseInt(cdbb.getRed6()));
         addNum("07", Integer.parseInt(cdbb.getRed7()));
         addNum("08", Integer.parseInt(cdbb.getRed8()));
         addNum("09", Integer.parseInt(cdbb.getRed9()));
         addNum("10", Integer.parseInt(cdbb.getRed10()));
         addNum("11", Integer.parseInt(cdbb.getRed11()));
         addNum("12", Integer.parseInt(cdbb.getRed12()));
         addNum("13", Integer.parseInt(cdbb.getRed13()));
         addNum("14", Integer.parseInt(cdbb.getRed14()));
         addNum("15", Integer.parseInt(cdbb.getRed15()));
         addNum("16", Integer.parseInt(cdbb.getRed16()));
         addNum("17", Integer.parseInt(cdbb.getRed17()));
         addNum("18", Integer.parseInt(cdbb.getRed18()));
         addNum("19", Integer.parseInt(cdbb.getRed19()));
         addNum("20", Integer.parseInt(cdbb.getRed20()));
         addNum("21", Integer.parseInt(cdbb.getRed21()));
         addNum("22", Integer.parseInt(cdbb.getRed22()));
         addNum("23", Integer.parseInt(cdbb.getRed23()));
         addNum("24", Integer.parseInt(cdbb.getRed24()));
         addNum("25", Integer.parseInt(cdbb.getRed25()));
         addNum("26", Integer.parseInt(cdbb.getRed26()));
         addNum("27", Integer.parseInt(cdbb.getRed27()));
         addNum("28", Integer.parseInt(cdbb.getRed28()));
         addNum("29", Integer.parseInt(cdbb.getRed29()));
         addNum("30", Integer.parseInt(cdbb.getRed30()));
         addNum("31", Integer.parseInt(cdbb.getRed31()));
         addNum("32", Integer.parseInt(cdbb.getRed32()));
         addNum("33", Integer.parseInt(cdbb.getRed33()));
      }
 }
}
public void addNum(String key,int value)
{
  summary.put(key,(summary.get(key)==null?0:summary.get(key))+value);
}
static double width=600;
static double height=480;
public void showme()
{
 table.setPrefSize(width-40, height-140);
//create stage which has set stage style transparent
final Stage stage = new Stage(StageStyle.UNDECORATED);
//create root node of scene, i.e. group
Group rootGroup = new Group();
//create scene with set width, height and color
Scene scene = new Scene(rootGroup, width,height, Color.TRANSPARENT);
//set scene to stage
stage.setScene(scene);
//center stage on screen
stage.centerOnScreen();
//show the stage
stage.show();



//when mouse button is pressed, save the initial position of screen
rootGroup.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
initX = me.getScreenX() - stage.getX();
initY = me.getScreenY() - stage.getY();
}
});

//when screen is dragged, translate it accordingly
rootGroup.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
stage.setX(me.getScreenX() - initX);
stage.setY(me.getScreenY() - initY);
}
});

// CREATE MIN AND CLOSE BUTTONS
//create button for closing application
Button close = new Button("关闭");
close.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.close();
}
});

//create button for minimalising application
Button min = new Button("缩小");
min.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.setIconified(true);
}
});

fixTable();
// CREATE SIMPLE TEXT NODE
Text text = new Text("JavaFX"); //20, 110,
text.setFill(Color.WHITESMOKE);
text.setEffect(new Lighting());
text.setBoundsType(TextBoundsType.VISUAL);
text.setFont(Font.font(Font.getDefault().getFamily(), 50));

HBox button=new HBox();
button.setSpacing(30);

button.getChildren().addAll(min,close);
// USE A LAYOUT VBOX FOR EASIER POSITIONING OF THE VISUAL NODES ON SCENE
VBox vBox = new VBox();
vBox.setSpacing(10);
vBox.setPadding(new Insets(60, 0, 0, 20));
vBox.setAlignment(Pos.TOP_CENTER);
vBox.getChildren().addAll(table, button);

//add all nodes to main root group
rootGroup.getChildren().addAll( vBox);    
}
public void fixTable()
{
        TableColumn keyCol = new TableColumn("数字");
        keyCol.setCellValueFactory(
    new PropertyValueFactory<Summary,String>("key")
);
        TableColumn statCol = new TableColumn("统计");    
        statCol.setCellValueFactory(
    new PropertyValueFactory<Summary,String>("statics")
);        
        table.getColumns().addAll(keyCol, statCol);  
        summary();
//fix person
  for(String key:summary.keySet())
  {
   data.add(new Summary(key,summary.get(key)));
  }
 System.out.println("data has size:"+data.size());  
  table.setItems(data);
}
    public static class Summary {
        private final SimpleStringProperty key;
        private final SimpleIntegerProperty statics;

        private Summary(String key, Integer value) {
            this.key = new SimpleStringProperty(key);
            this.statics = new SimpleIntegerProperty(value);
        }

        public String getKey() {
            return key.get();
        }
        public void setKey(String value) {
            key.set(value);
        }
       
        public Integer getStatics() {
            return statics.get();
        }
        public void setStatics(Integer value) {
            statics.set(value);
        }
    }
}
